<template>
  <div>
    <div class="breadcrumb-box">
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item :to="{ path: '/order' }">订单</el-breadcrumb-item>
        <el-breadcrumb-item>申请售后</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container refund-page">
      <table>
        <tbody>
          <tr class="tr-th">
            <td colspan="2">
              <div class="order-top td-fbc">
                <div class="td-fsc">
                  <span>{{orderInfo._add_time}}</span>
                  <span>订单编号：{{orderInfo.order_id}}</span>
                </div>
                <div class="td-fsc">
                  <img :src="$global.Image.icon_serve" />
                  <span>联系客服</span>
                </div>
              </div>
            </td>
          </tr>
          <tr class="tr-tbd" v-for="goods in orderInfo.cartInfo" :key="goods.id">
            <td>
              <div
                class="goods-box td-fss"
                @click="$router.push({ path: '/orderDetails/' + order.order_id })"
              >
                <div class="goods-img">
                  <img
                    :src="goods.productInfo.image"
                    @click.stop=" $router.push({ path: '/detail/' + goods.productInfo.id })"
                  />
                </div>
                <div class="goods-info td-f1">
                  <div
                    class="tit"
                    @click.stop=" $router.push({ path: '/detail/' + goods.productInfo.id })"
                  >{{goods.productInfo.store_name}}</div>
                  <!-- <div class="attr">灰色</div> -->
                </div>
                <div class="goods-num">
                  <small>x</small>
                  <span>{{goods.cart_num}}</span>
                </div>
              </div>
            </td>
          </tr>
        </tbody>
      </table>
      <div class="refund-form">
        <el-form label-width="120px">
          <el-form-item label="退款件数">
            <el-input v-model="orderInfo.total_num" readonly></el-input>
          </el-form-item>
          <el-form-item label="退款金额">
            <el-input v-model="orderInfo.total_price" readonly></el-input>
          </el-form-item>
        </el-form>
        <el-form
          :model="refundForm"
          :rules="rules"
          ref="refundForm"
          label-width="120px"
          class="demo-refundForm"
        >
          <el-form-item label="退款原因" prop="reason">
            <el-select v-model="refundForm.reason" placeholder="请选择退款原因">
              <el-option
                v-for="(item,index) in reasonList"
                :key="index"
                :label="item"
                :value="item"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="备注说明" prop="remark">
            <el-input type="textarea" v-model="refundForm.remark"></el-input>
          </el-form-item>
          <div data-v-43e78bf1 class="el-form-item">
            <label class="el-form-item__label" style="width: 120px;">上传图片</label>
            <div class="el-form-item__content upload-img" style="margin-left: 120px;">
              <el-upload
                ref="upload"
                :action="url"
                :data="qiniuData"
                list-type="picture"
                :limit="3"
                :on-preview="handlePictureCardPreview"
                :before-upload="beforeAvatarUpload"
                :on-remove="handleRemove"
                :on-success="handleAvatarSuccess"
              >
                <el-button size="small" type="primary">选择图片</el-button>
                <div slot="tip" class="el-upload__tip">最多可上传3张，单张图片最大不超过10M</div>
              </el-upload>
              <el-dialog :visible.sync="dialogVisible" size="tiny">
                <img width="100%" :src="dialogImageUrl" alt />
              </el-dialog>
            </div>
          </div>
          <el-form-item>
            <el-button class="btn-r" type="primary" @click="submitForm('refundForm')">申请退款</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import { orderDetail, getRefundReason, postOrderRefund } from "@api/order";
import { qiniuToken } from "@api/public";
import { trim, VUE_APP_API_URL } from "@utils";

export default {
  name: "goodsReturn",
  data() {
    return {
      id: this.$route.params.id || 0,
      orderInfo: {},
      goodsList: [],
      reasonList: [],
      refund_reason_wap_img: [],
      refundForm: {
        reason: "",
        remark: ""
      },
      rules: {
        reason: [
          { required: true, message: "请选择退款原因", trigger: "change" }
        ],
        remark: [
          {
            required: true,
            message: "请填写备注信息，100字以内",
            trigger: "blur"
          },
          { min: 0, max: 150, message: "内容超出限制", trigger: "blur" }
        ]
      },
      // 上传地址
      url: 'http://upload-z2.qiniup.com',
      dialogImageUrl: "",
      dialogVisible: false,
      qiniuDomail: '',
      qiniuData: {},
      fileList: [], // 上传文件列表
      fileType: ["image/png", "image/jpg", "image/jpeg", "image/gif"]
    };
  },
  mounted() {
    this.getQiniuToken()
    this.getOrderDetail();
    this.getRefundReason();
  },
  methods: {
    getQiniuToken () {
      qiniuToken()
        .then(res => {
          this.qiniuData.token = res.data.uptoken
          this.qiniuDomail = res.data.pc_domain
        })
        .catch(err => {
          this.$dialog.error(err.msg || "获取七牛数据失败");
        });
    },
    getOrderDetail() {
      orderDetail(this.id)
        .then(res => {
          this.orderInfo = res.data;
          this.goodsList = res.data.cartInfo || [];
        })
        .catch(err => {
          this.$dialog.error(err.msg || "获取订单失败");
        });
    },
    getRefundReason() {
      getRefundReason().then(res => {
        this.reasonList = res.data;
      });
    },
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.submit();
        } else {
          // console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    submit() {
      let refund_reason_wap_explain = trim(this.refundForm.remark),
        text = this.refundForm.reason;
      let refund_reason_wap_img  = []
      refund_reason_wap_img = this.fileList.map((item)=>{
        return this.qiniuDomail + '/' + item.response.key
      })
      postOrderRefund({
        text,
        uni: this.orderInfo.order_id,
        refund_reason_wap_img: refund_reason_wap_img.join(","),
        refund_reason_wap_explain
      })
        .then(res => {
          this.$dialog.success(res.msg);
          this.$router.go(-1);
        })
        .catch(res => {
          this.$dialog.error(res.msg);
        });
    },
    // upload img
    handleRemove(file, fileList) {
      this.fileList = fileList;
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    handleAvatarSuccess(res, file, fileList) {
      this.fileList = fileList;
    },
    beforeAvatarUpload(file) {
      let that = this;
      const isImage = this.fileType.indexOf(file.type) > -1;
      const isLt20M = file.size / 1024 / 1024 < 20;

      if (!isImage) {
        this.$dialog.error('请上传图片类型文件');
      }
      if (!isLt20M) {
        this.$dialog.error('请上传小于20M文件！');
      }
      return isImage && isLt20M;
    }
  }
};
</script>

<style scoped  lang="scss" scoped>
$color: #DA213D;
.refund-page {
  padding-bottom: 100px;
  table {
    width: 100%;
    border-collapse: collapse;
    .sep-row {
      div {
        height: 20px;
      }
    }
    .tr-th {
      background-color: #e5e5e5;
      border: solid #e5e5e5;
      border-width: 1px 1px 0 1px;
    }
    .tr-tbd {
      border: solid #e5e5e5 1px;
    }
    .goods-box {
      padding: 10px;
    }
    td {
      border: 1px solid #e5e5e5;
    }
    thead tr {
      background-color: #e5e5e5;
      th {
        font-size: 13px;
        color: #666;
        padding: 10px 0;
      }
    }
    .money .status {
      display: inline-block;
      border-top: solid #eee 1px;
      padding-top: 10px;
      margin-top: 10px;
      width: 60px;
      text-align: center;
    }
  }
  .tx-c {
    text-align: center;
  }
  .order-top {
    padding: 10px;
    span {
      font-size: 12px;
      color: #666;
      margin-right: 20px;
    }
    img {
      display: block;
      margin-right: 8px;
      width: 14px;
      height: 14px;
      object-fit: contain;
    }
  }
  .goods-img {
    width: 60px;
    height: 60px;
    margin-right: 10px;
    cursor: pointer;
    img {
      display: block;
      width: 100%;
      height: 100%;
      object-fit: cover;
    }
  }
  .goods-info {
    width: 260px;
    font-size: 12px;
    .tit {
      color: #333;
      font-size: 13px;
      line-height: 1.6;
      width: 400px;
      cursor: pointer;
    }
    .attr {
      margin-top: 5px;
      color: #999;
    }
  }
  .goods-num {
    width: 60px;
    text-align: center;
    color: #999;
    span {
      color: #333;
      font-size: 14px;
    }
  }
  .btn-div {
    .bnt {
      padding: 5px 0;
      cursor: pointer;
    }
    .bnt:hover {
      color: $color;
    }
    .bg-color-red {
      color: $color;
    }
  }
}
.refund-form {
  margin: 60px auto;
  width: 500px;
  .el-select {
    width: 100%;
  }
}
.upload-img {
  .pictrue {
    width: 80px;
    height: 80px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    border: dashed #dbdbdb 1px;
    border-radius: 4px;
    img {
      display: block;
      width: 40px;
      height: 40px;
    }
    .text {
      font-size: 14px;
      color: #dbdbdb;
      line-height: 20px;
    }
  }
}
</style>